﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ACNetMusic
{
    public partial class MainScreen : Form
    {
        private ServerController serverCtrl;
        private SongController songCtrl;
        private ClientController clientCtrl;
        private ArrayList clientsList;
        private ArrayList songsList;

        
        public MainScreen()
        {
            InitializeComponent();
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                serverCtrl.start();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Initializing server failed!", "Error");
            }
        }
        

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // stop & disposing threads, stop listening.
            serverCtrl.stop();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            serverCtrl.stop();
            this.Close();
        }

        private void start_Click(object sender, EventArgs e)
        {
            serverCtrl.start();
        }

        private void stop_Click(object sender, EventArgs e)
        {
            serverCtrl.stop();
        }

        private void show_clients_Click(object sender, EventArgs e)
        {
            tabControl.SelectedIndex = 0;
        }

        private void show_songs_list_Click(object sender, EventArgs e)
        {
            tabControl.SelectedIndex = 1;
        }

        private void reloadClients()
        {
            clientsList = clientCtrl.getClientsList();
            dgClients.DataSource = clientsList;
        }

        private void reloadSongs()
        {
            songsList = songCtrl.getSongsList();
            dgSongs.DataSource = songsList;
        }

        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (tabControl.SelectedIndex) 
            {
                case 0:
                    reloadClients();
                    break;
                case 1:
                    reloadSongs();
                    break;
            }
        }

        private void MainScreen_Load(object sender, EventArgs e)
        {
            clientCtrl = new ClientController();
            serverCtrl = new ServerController();
            songCtrl = new SongController();
            clientsList = clientCtrl.getClientsList();
            dgClients.DataSource = clientsList;
        }
    }
}
